﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ROrganizer.Client;

namespace ROrganizer.Client.Crawling
{
    public class MessageRepository
    {
        public const string Name = "MesgRepository.xml";

        public MessageEntry Get(string source)
        {
            using (ApplicationData.OpenSession(MessageRepository.Name))
            {
                var document = ApplicationData.GetDocument(MessageRepository.Name);
                var serializer = new MessageSerializer();
                var entry = serializer.Deserialize(document, source);

                return entry;
            }
        }

        public MessageEntry[] All()
        {
            using (ApplicationData.OpenSession(MessageRepository.Name))
            {
                var document = ApplicationData.GetDocument(MessageRepository.Name);
                var serializer = new MessageSerializer();
                var entries = serializer.Deserialize(document);

                return entries;
            }
        }

        public void SaveOrUpdate(params MessageEntry[] entries)
        {
            using (ApplicationData.OpenSession(MessageRepository.Name))
            {
                var document = ApplicationData.GetDocument(MessageRepository.Name);
                var serializer = new MessageSerializer();

                serializer.Update(document, entries);
                ApplicationData.UpdateDocument(MessageRepository.Name, document);
            }
        }
    }
}
